<?php
/**
 * 物品控制器
 * 
 * @category QuickBug
 * @copyright http://www.vquickbug.com
 */
class UserController extends BaseController
{
	/**
	 * 模型
	 *
	 * @var unknown_type
	 */
	private $model;
	
	/**
	 * 自动运行
	 */
	public function init(){
		parent::init();
		$this->model = new Model_User();
	}

	/**
	 * 首页
	 */
	public function listAction(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$offset = ($page-1)*$rows;
		$result = array();
		$result["total"] = $this->model->countUser();
		$result["rows"] = $this->model->listUser($offset,$rows);
	
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function addAction(){
		$param = array(
			'username'=>$this->request->getPost('username'),
			'password'=>md5($this->request->getPost('new-password')),
			'sirname'=>$this->request->getPost('sirname'),
			'givenname'=>$this->request->getPost('givenname'),
			'dept_id'=>intval($this->request->getPost('dept_id')),
			'persona_id'=>$this->request->getPost('persona_id'),
			'tel'=>$this->request->getPost('tel'),
			'mob'=>$this->request->getPost('mob'),
			'is_operator'=>$this->request->getPost('is_operator')?1:0,
		);
		
		$result = $this->model->addUser($param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'添加用户失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function updateAction(){
		$id = intval($this->request->getGet('id'))?intval($this->request->getGet('id')):0;
		$param = array(
			'sirname'=>$this->request->getPost('sirname'),
			'givenname'=>$this->request->getPost('givenname'),
			'dept_id'=>intval($this->request->getPost('dept_id')),
			'persona_id'=>$this->request->getPost('persona_id'),
			'tel'=>$this->request->getPost('tel'),
			'mob'=>$this->request->getPost('mob'),
			'is_operator'=>$this->request->getPost('is_operator')?1:0,
			'is_locked'=>$this->request->getPost('is_locked')?1:0,
		);
		if($this->request->getPost('new-password')){
			$param['password'] = md5($this->request->getPost('new-password'));
		}
		
		$result = $this->model->updateUser($id,$param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'编辑用户失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function delAction(){
		$id = intval($this->request->getPost('id'))?intval($this->request->getPost('id')):0;
		$result = $this->model->delUser($id);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'删除用户失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function passwordAction(){
		$id = $this->userid;
		$password0 = $this->request->getPost('password0');
		$password1 = $this->request->getPost('password1');
		$password2 = $this->request->getPost('password2');
		if(empty($id) || empty($password1) || ($password1 !=$password2)){
			echo json_encode(array('msg'=>'账户或新设密码错误'));
		}
		elseif(!$this->model->auth($id,$password0)){
			echo json_encode(array('msg'=>'原始密码错误'));
		}
		else{
			$param = array('password' => md5($password1));
			$result = $this->model->updateUser($id,$param);
			if ($result){
				echo json_encode(array('success'=>true));
			} else {
				echo json_encode(array('msg'=>'修改密码失败'));
			}
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
}